package org.redis.cluster.transport.netty.test;

import java.net.InetSocketAddress;

import org.junit.Test;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import redis.clients.jedis.Jedis;

public class NettyTester_2 {

	@Test
	public void test() throws Exception {
		EventLoopGroup bossGroup=new NioEventLoopGroup(1);
		EventLoopGroup workerGroup=new NioEventLoopGroup(4);
        
    	ServerBootstrap serverBootstrap=new ServerBootstrap();
		serverBootstrap.group(bossGroup,workerGroup);
		serverBootstrap.channel(NioServerSocketChannel.class);
		
		serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel socketChannel) throws Exception {
				ChannelPipeline pipeline=socketChannel.pipeline();
				pipeline.addLast("decoder", new TestDecoder());  
		        pipeline.addLast("encoder", new TestEncoder());  
		        pipeline.addLast("handler", new TestProcessor_2());
			}
		});
		
		serverBootstrap.option(ChannelOption.SO_KEEPALIVE, true);
		serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
		serverBootstrap.option(ChannelOption.TCP_NODELAY, true);
		serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
		
		ChannelFuture channelFuture=serverBootstrap.bind(new InetSocketAddress(6379)).sync();
		
		Jedis jedis=new Jedis("127.0.0.1",6379,1000*1);
		
		for(int j=0;j<20;j++) {
			long start=System.currentTimeMillis();
			
			for(int i=0;i<1000;i++) {
				jedis.ping();
			}
			System.out.println(System.currentTimeMillis()-start);
		}
	}
}